package com.ihmp.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ihmp.business.HotelService;
import com.ihmp.business.ProductInRoomService;
import com.ihmp.model.Tproductinrooms;


@Controller
public class ProductInRoomCatalog {
	
	@Autowired
	private ProductInRoomService productInRoomService;
	@Autowired
	private HotelService hotelService;

	@RequestMapping(value="/accommodation/product-in-room-catalog.html",method=RequestMethod.GET)
	public ModelAndView listProductInRooms(@ModelAttribute("command") Tproductinrooms product){
		Map<String, Object> model = new HashMap<String, Object>();		
		model.put("pageTitle", "Đặt phòng cho khách lẻ - iHMP DanaTours");
		model.put("productInRooms", productInRoomService.listProductInRoom());	
		model.put("hotelList", hotelService.list());		
		return new ModelAndView("accommodation/product-in-room-catalog",model);
	}
	
	@RequestMapping(value="/accommodation/add-product-in-room.html",method=RequestMethod.POST)
	public ModelAndView addProduct(@ModelAttribute("command") Tproductinrooms product,BindingResult result,
								   @RequestParam Integer hotelId){					
		product.setHotel(hotelService.get(hotelId));
		product.setCreater(1);
		product.setCreateDate(new Date(System.currentTimeMillis()));
		productInRoomService.saveOrUpdateProductInRoom(product);
		return new ModelAndView("redirect:product-in-room-catalog.html");
	}
	
	@RequestMapping(value="/accommodation/update-product-in-room.html",method=RequestMethod.GET)
	public ModelAndView updateProduct(@ModelAttribute("command") Tproductinrooms product,BindingResult result,
									  @RequestParam Integer productInRoomId){
		Map<String, Object> model = new HashMap<String, Object>();	
		product = productInRoomService.getProductInRoom(productInRoomId);		
		model.put("product",product);
		model.put("pageTitle", "Đặt phòng cho khách lẻ - iHMP DanaTours");
		model.put("productInRooms", productInRoomService.listProductInRoom());	
		model.put("hotelList", hotelService.list());
		return new ModelAndView("accommodation/product-in-room-catalog",model);
	}
	
	@RequestMapping(value="/accommodation/delete-product-in-room.html",method=RequestMethod.GET)
	public ModelAndView deleteProduct(@ModelAttribute("command") Tproductinrooms product,BindingResult result){
		productInRoomService.deleteProductInRoom(product);
		return new ModelAndView("redirect:product-in-room-catalog.html");
	}		
}
